package com.lisx.module_memo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.fenghuajueli.lib_data.constants.EventBusConstants;
import com.fenghuajueli.lib_data.entity.db.MemoDbEntity;
import com.fenghuajueli.lib_data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.manager.MemoManager;
import com.fenghuajueli.libbasecoreui.mvp.OtherBaseFragment;
import com.lisx.module_memo.R;
import com.lisx.module_memo.activity.AddMemoActivity;
import com.lisx.module_memo.activity.MemoActivity;
import com.lisx.module_memo.adapter.MemoListAdapter;
import com.lisx.module_memo.databinding.FragmentMemoItemBinding;
//import com.umeng.commonsdk.debug.I;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MemoItemFragment extends OtherBaseFragment {

    private FragmentMemoItemBinding binding;
    private int ptType = 1;
    private MemoListAdapter adapter;

    @Override
    public void initView(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        binding = FragmentMemoItemBinding.bind(getContentView());

        Bundle arguments = getArguments();
        if (arguments != null) {
            ptType = arguments.getInt("ptType", 1);
        }

        binding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MemoListAdapter();
        binding.recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new MemoListAdapter.OnItemClickListener() {
            @Override
            public void onEdit(MemoDbEntity memoDbEntity) {
                Intent intent = new Intent(getContext(), AddMemoActivity.class);
                intent.putExtra("ptType", ptType);
                intent.putExtra("data",memoDbEntity);
                startActivity(intent);
            }

            @Override
            public void onModifyMemo(MemoDbEntity memoDbEntity, int modify) {
                modifyMemo(memoDbEntity, modify);
            }

        });

        setAdapterData();
    }

    private void modifyMemo(MemoDbEntity memoDbEntity, int modify) {
        Observable.create(new ObservableOnSubscribe<List<MemoDbEntity>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<MemoDbEntity>> emitter) throws Exception {
                List<MemoDbEntity> memoData;
                if (modify == 1) {
                    memoData = MemoManager.getInstance().deleteMemoDbEntity(memoDbEntity);
                } else {
                    memoData = MemoManager.getInstance().updateMemoDbEntity(memoDbEntity);
                }
                emitter.onNext(memoData);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<MemoDbEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<MemoDbEntity> list) {
                        if (list != null && list.size() > 0) {
                            adapter.setData(list);
                            binding.recyclerview.setVisibility(View.VISIBLE);
                            binding.noDataContainer.setVisibility(View.GONE);
                        } else {
                            binding.recyclerview.setVisibility(View.GONE);
                            binding.noDataContainer.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void setAdapterData() {
        Observable.create(new ObservableOnSubscribe<List<MemoDbEntity>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<MemoDbEntity>> emitter) throws Exception {
                List<MemoDbEntity> memoData = MemoManager.getInstance().getMemoData(ptType);
                emitter.onNext(memoData);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<MemoDbEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<MemoDbEntity> list) {
                        if (list != null && list.size() > 0) {
                            adapter.setData(list);
                            binding.recyclerview.setVisibility(View.VISIBLE);
                            binding.noDataContainer.setVisibility(View.GONE);
                        } else {
                            binding.recyclerview.setVisibility(View.GONE);
                            binding.noDataContainer.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(EventEntity eventBusEntity) {
        if (eventBusEntity.getCode() == EventBusConstants.UPDATE_MEMO) {
            setAdapterData();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_memo_item;
    }
}
